﻿using System;
using System.Collections.Generic;
using System.Linq;
using Model.Context;
using Model.Entity;
using Ones.Dal.Repository.Interfaces;

namespace Ones.Dal.Repository.Impl
{
    public class NewsDao : AbstractDao<News>, INewsDao
    {
        public NewsDao()
        {
            _ctx = new OnlineNewsEditorialDataContext();
        }

        public NewsDao(OnlineNewsEditorialDataContext ctx)
        {
            _ctx = ctx;
        }

        public override bool
        update(News news)
        {
            try
            {
                var curr = getById(news.newsId);

                curr.catId = news.catId;
                curr.newsTitle = news.newsTitle;
                curr.newsIntroduceText = news.newsIntroduceText;
                curr.newsContent = news.newsContent;
                curr.author = news.author;
                curr.modificationDate = news.modificationDate;
                curr.processStatus = news.processStatus;
                curr.verified = news.verified;
                curr.publisher = news.publisher;
                curr.publishDate = news.publishDate;
                curr.unpublishDate = news.unpublishDate;
                curr.numberOfView = news.numberOfView;
                curr.newsExclusion = news.newsExclusion;
                
                _ctx.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Set News exclusion is true to remove news from News View
        /// </summary>
        /// <param name="newsId"></param>
        /// <returns></returns>
        public bool remove(int newsId)
        {
            try 
            {
                var q = (from n in _ctx.News where n.newsId == newsId select n).SingleOrDefault();
                q.newsExclusion = true;
                _ctx.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
            
        }
      
        /// <summary>
        /// Delete permanently news in news table
        /// </summary>
        /// <param name="newsId"></param>
        /// <returns></returns>
        public bool delete(int newsId)
        {
            try
            {
                var q = getById(newsId);
                _ctx.News.DeleteOnSubmit(q);
                _ctx.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }

        }

        public override News
        getById(object id)
        {
            return getAll().Where(i => i.newsId == (int)id).SingleOrDefault();
        }

        public List<News>
        getByCategory(string catId)
        {
            try
            {
                var q = from n in _ctx.News
                        where n.catId.Equals(catId)
                        select n;
                return q.ToList<News>();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<News>
        getByTitle(string newsTitle)
        {
            try
            {
                var q = from n in _ctx.News
                        where n.newsTitle.Contains(newsTitle)
                        select n;
                return q.ToList<News>();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<News>
        getByAuthor(string authorId)
        {
            try
            {
                var q = from n in _ctx.News
                        where n.author.Contains(authorId)
                        select n;
                return q.ToList<News>();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<News>
        getByPostingDate(DateTime postingDate)
        {
            try
            {
                var q = from n in _ctx.News
                        where n.postingDate.Equals(postingDate)
                        select n;
                return q.ToList<News>();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<News>
        getByModificationDate(DateTime modificationDate)
        {
            try
            {
                var q = from n in _ctx.News
                        where n.modificationDate.Equals(modificationDate)
                        select n;
                return q.ToList<News>();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<News>
        getByProcessStatus(string processStatus)
        {
            try
            {
                if (processStatus.Equals("Approved") || processStatus.Equals("Edited") || processStatus.Equals("Posted") || processStatus.Equals("RejectedA") || processStatus.Equals("RejectedE"))
                {
                    var q = from n in _ctx.News
                            where n.processStatus.Equals(processStatus)
                            select n;
                    return q.ToList<News>();
                }
                else
                {
                    return null;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<News>
        getByVerified(bool verified)
        {
            try
            {
                var q = from n in _ctx.News
                        where n.verified == verified
                        select n;
                return q.ToList<News>();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<News>
        getByPublisher(string publisherId)
        {
            try
            {
                var q = from n in _ctx.News
                        where n.publisher.Equals(publisherId)
                        select n;
                return q.ToList<News>();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<News>
        getByPublishDate(DateTime publishDate)
        {
            try
            {
                var q = from n in _ctx.News
                        where n.publishDate.Equals(publishDate)
                        select n;
                return q.ToList<News>();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<News>
        getByUnpublishDate(DateTime unpublishDate)
        {
            try
            {
                var q = from n in _ctx.News
                        where n.unpublishDate.Equals(unpublishDate)
                        select n;
                return q.ToList<News>();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<viewNews> 
        getAllAvailableNews(bool availableNews)
        {
            var q = from n in _ctx.viewNews
                    where n.newsExclusion == availableNews
                    select n;
            return q.ToList<viewNews>();
        }
    }

    #region INewsDao

    public interface INewsDao : IDao<News>
    {
        bool delete(int newsId);
        List<News> getByCategory(String catId);

        List<News> getByTitle(String newsTitle);

        List<News> getByAuthor(String authorId);

        List<News> getByPostingDate(DateTime postingDate);

        List<News> getByModificationDate(DateTime modificationDate);

        List<News> getByProcessStatus(String processStatus);

        List<News> getByVerified(bool verified);

        List<News> getByPublisher(String publisherId);

        List<News> getByPublishDate(DateTime publishDate);

        List<News> getByUnpublishDate(DateTime unpublishDate);

        List<viewNews> getAllAvailableNews(bool availableNews);

        bool remove(int newsId);

    }
    #endregion
}